import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import {
  addCartAPI,
  getCartAPI,
  delCartAPI,
  mergeCartAPI
} from '@/api/detail.js'
import { useUserStore } from './user'
export const useCartStore = defineStore(
  'cart',
  () => {
    const userStore = useUserStore()

    const cartList = ref([
      {
        id: '4000277',
        skuId: '300283140',
        name: '21春季新品，宝宝舒适四季连体衣0-3岁',
        attrsText: '尺码:90cm（建议2-3岁） 颜色:字母波浪 ',
        specs: [],
        picture:
          'https://yanxuan-item.nosdn.127.net/6efe85e63ab1e09896e1c99a9fe09d9e.png',
        price: '79.00',
        nowPrice: '79.00',
        nowOriginalPrice: '79.00',
        selected: true,
        stock: 4654,
        count: 9,
        isEffective: true,
        discount: null,
        isCollect: false,
        postFee: 9
      }
    ])
    //合并购物车
    const merge = async () => {
      const data = ref([])
      cartList.value.forEach((item) => {
        data.value.push({
          skuId: item.skuId,
          selected: item.selected,
          count: item.count
        })
      })
      await mergeCartAPI(data.value)
      getCartList()
    }
    const deleteAll = () => {
      cartList.value = []
    }
    const getCartList = async () => {
      //获得购物车数据
      const res = await getCartAPI()
      cartList.value = res.data.result
      cartList.value.forEach((i) => {
        i.totalPrice = i.nowPrice * i.count
      })
    }
    const clearCartList = async () => {
      //群删购物车数据
      if (userStore.userInfo.token) {
        const items = cartList.value.filter((i) => i.selected)
        console.log(12345, items)

        const ids = items.map((i) => i.skuId)
        console.log(12345, ids)
        await delCartAPI(ids)
        getCartList()
      } else {
        cartList.value = cartList.value.filter((i) => !i.selected)
        console.log(cartList.value)
      }
    }
    //删除一行数据
    const delCartList = async (id) => {
      if (userStore.userInfo.token) {
        await delCartAPI([id])
        getCartList()
      } else {
        cartList.value = cartList.value.filter((i) => i.skuId !== id)
      }
    }
    const addCart = async (goods) => {
      if (userStore.userInfo.token) {
        const { skuId, count } = goods
        await addCartAPI({ skuId, count })
        getCartList()
      } else {
        const res = cartList.value.find(
          (i) => i.id === goods.id && i.skuId === goods.skuId
        )
        if (res) {
          res.count += goods.count
        } else {
          cartList.value.push(goods)
        }
      }
    }
    const totalPrice = computed(() =>
      cartList.value?.reduce((sum, item) => sum + item.price * item.count, 0)
    )
    const totalNum = computed(() =>
      cartList.value?.reduce((sum, item) => sum + item.count, 0)
    )
    const selectPrice = computed(() => {
      const list = ref(cartList.value.filter((item) => item.selected === true))
      return list.value.reduce(
        (sum, item) => (sum = sum + item.count * item.price),
        0
      )
    })
    const selectCount = computed(() => {
      const list = ref(cartList.value.filter((item) => item.selected === true))
      return list.value.reduce((sum, item) => (sum = sum + item.count), 0)
    })
    const allSelect = computed(() => {
      return cartList.value.every((i) => i.selected)
    })
    const changeAll = () => {
      if (allSelect.value) {
        cartList.value.forEach((i) => {
          i.selected = false
        })
      } else {
        cartList.value.forEach((i) => {
          i.selected = true
        })
      }
    }
    return {
      changeAll,
      allSelect,
      cartList,
      deleteAll,
      clearCartList,
      getCartList,
      totalPrice,
      totalNum,
      addCart,
      selectPrice,
      selectCount,
      merge,
      delCartList
    }
  },
  { persist: true }
)
